package com.bps.zzant.json;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.bps.zzant.model.ArtFile;
import com.bps.zzant.model.Email;
import com.bps.zzant.services.EmailService;
import com.bps.zzant.services.EmailServiceImpl;
import com.bps.zzant.services.FileService;



@Controller
public class EmailJSON<K> {
	
	private static final Logger logger = LoggerFactory.getLogger(EmailJSON.class);
	
	@Autowired
	private EmailServiceImpl emailService;
	@Autowired
	private FileService fileService;
		
	@RequestMapping(value = "/sendEmail.json/{whatEmail}/{whatId}", method = RequestMethod.GET)
    public Map sendEmail(HttpServletResponse response,@PathVariable String whatEmail, @PathVariable String whatId){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", new Boolean("true"));
		
		logger.info(" +++++++in EmailJSON.sendEmail.json" + whatEmail + ". order id: " + whatId );
		emailService.sendSimpleMessage("jim.zhu@na.nyklogistics.com", "yzhu2000@yahoo.com", "icis test", "I am test icis.");
	
		return map;
		
    }
	
	/*
	* Send HTML mail with inline image
	*/
	@RequestMapping(value = "/emailQuote", method = RequestMethod.POST)
	public Map emailQuote(HttpServletRequest request) throws MessagingException, IOException {
		
		logger.info(" ++++++++++++++++++   in EmailJSON.emailQuote  +++++++++++++++++");
//		logger.info(" ++++++++++++++++++ order id:" + request.getParameter("emailOrderId"));
//		logger.info(" ++++++++++++++++++ contact:" + request.getParameter("contact"));
//		logger.info(" ++++++++++++++++++ email:" + request.getParameter("email"));
//		logger.info(" ++++++++++++++++++ content:" + request.getParameter("message"));
		
		String orderId = request.getParameter("emailOrderId");
		String customerId = request.getParameter("customerId");
		
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> mergeMap = new HashMap<String, Object>();
		
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
	    String userName = auth.getName(); //get logged in username

	    String[] requestURL = request.getRequestURI().split("/");
		 StringBuffer url = new StringBuffer();
		 for(int i=0; i<requestURL.length-1; i++){
			 url.append(requestURL[i]);
		 }
		 // publicvieworder/487/73
		 String logoUrl = "http://" + request.getServerName()+":"+request.getServerPort()+"/"+url+"/resources/theme/images/big_logo.jpg";
		 String viewButtonImage = "http://" + request.getServerName()+":"+request.getServerPort()+"/"+url+"/resources/theme/images/view_button.jpg";
		 String viewButtonUrl = "http://" + request.getServerName()+":"+request.getServerPort()+"/"+url+"/publicvieworder/"+customerId+"/"+orderId;
		 // logger.info("logoUrl:" + logoUrl);
		 mergeMap.put("logo", logoUrl);
		 mergeMap.put("viewbutton", viewButtonImage);
		 mergeMap.put("viewbuttonurl", viewButtonUrl);
		 mergeMap.put("user", userName);
		 mergeMap.put("customerName", request.getParameter("contact"));
		 mergeMap.put("content", StringUtils.isEmpty(request.getParameter("message"))?"":request.getParameter("message")+"<br><br>");
		
		Email quoteEmail = new Email();
		quoteEmail.setFromEmailAddress("jim.zhu@na.nyklogistics.com");
		quoteEmail.setReplyToEmailAddress("promo88@aol.com");
		quoteEmail.setToEmailAddresses(request.getParameter("email"));
		quoteEmail.setSubject("Re: Quotation from Promo AD Sales");
		quoteEmail.setTemplate("quotetemplate.vm");
		quoteEmail.setTemplateMergeModel(mergeMap);
		
		map.put("success", new Boolean("true"));
		
		Locale locale = new Locale("en", "US");
		 
		// this.emailService.emailQuote(request.getParameter("email"), "jim.zhu@na.nyklogistics.com", "Re: Quotation", request.getParameter("message"), request);
		this.emailService.sendEmail(quoteEmail);
		
		return map;
	 
	}
	
}
